class UsersController < ApplicationController
  include UsersHelper
  include CrudHelper

  before_filter :check_permissions_write, :only => [:edit,:update]
  skip_before_filter :require_no_authentication

  def check_permissions_read
    authorize! :read, :users
  end

  def check_permissions_write
    authorize! :write, :users
  end

  def new
    @entity = create_entity()
    @roles = Role.all

    respond_to do |format|
      format.html
      format.json { render :json => @entity }
    end
  end

  def create
     @user = User.new(params[:user])
     @roles = Role.all
     respond_to do |format|
       if @user.save
         format.html  { redirect_to(@user,
                                    :notice => I18n.t("user.created.success")) }
         format.json  { render :json => @user,
                               :status => :created, :location => @post }
       else
         format.html  { render :action => "new" }
         format.json  { render :json => @user.errors,
                               :status => :unprocessable_entity }
       end
     end
  end

  def show
     @user = User.find(params[:id])

     respond_to do |format|
       format.html
       format.json { render :json => @users }
     end
  end

  def update
    @user = User.find(params[:id])
  end

  def destroy
    @user = User.find(params[:id])
    @user.destroy

    respond_to do |format|
      format.html { redirect_to users_path }
      format.json { head :no_content }
      format.js
    end
  end

  def edit
    @entity = User.find(params[:id])
    @roles = Role.all
  end
end
